﻿using System;

namespace ObserverPattern.DelegateImplement;

// 委托充当订阅者接口类
public delegate void NotifyEventHandler(object sender);

// 抽象订阅号类
public class Media(string symbol, string info)
{
    private NotifyEventHandler? _notifyEvent;
    public string Symbol { get; } = symbol;
    public string Info { get; } = info;

    #region 新增对订阅号列表的维护操作

    public void AddObserver(NotifyEventHandler observer) {_notifyEvent += observer;}

    public void RemoveObserver(NotifyEventHandler observer) { _notifyEvent -= observer; }

    #endregion

    public void Update() { _notifyEvent?.Invoke(this); }
}

// 具体订阅号类
public class TencentGame(string symbol, string info) : Media(symbol, info);

// 具体订阅者类
public class Subscriber(string name)
{
    private string Name { get; } = name;

    public void ReceiveAction(object obj)
    {
        if (obj is Media tencent)
        {
            Console.WriteLine("\"{0}\"收到来自\"{1}\"的通知\"{2}\"", Name, tencent.Symbol, tencent.Info);
        }
    }
}
